package service

import (
	"api-wareway/basic/config"
	__ "api-wareway/basic/proto"
	"api-wareway/handler/request"
	"fmt"
	"github.com/Baidu-AIP/golang-sdk/aip/censor"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"log"
	"net/http"
	"path/filepath"
	"strings"
)

func UploadFile(c *gin.Context) {
	// 单文件
	file, _ := c.FormFile("file")

	etc := strings.ToTitle(filepath.Ext(file.Filename))

	if !strings.Contains(".mp4", etc) {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "文件格式应为mp4",
		})
		return
	}

	//审核
	client := censor.NewClient("ak", "sk")
	res := client.TextCensor(file.Filename)
	fmt.Println(res)

	if !strings.Contains("合格", res) {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "视频文件名违规",
		})
		return
	}

	file.Filename = uuid.NewString() + etc

	log.Println(file.Filename)

	url := "./file" + file.Filename
	// 上传文件至指定的完整文件路径

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "文件上传成功",
		"data": url,
	})
}

func AddVideo(c *gin.Context) {
	var form request.AddVideoForm

	err := c.ShouldBind(&form)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}

	video, err := config.VideoClient.AddVideo(c, &__.AddVideoReq{
		Title:       form.Title,
		VideoListId: int64(form.VideoListId),
		VideoUrl:    form.VideoUrl,
		VideoTypeId: int64(form.VideoTypeId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "添加失败",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": video,
	})

}

func AddVideoList(c *gin.Context) {
	var form request.AddVideoForm

	err := c.ShouldBind(&form)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}

}
